#include <iostream>
#include <unordered_map>
#include <fstream>
#include <sstream>
#include <string>

using std::cout;
using std::endl;
using std::cerr;
using std::unordered_map;
using std::istringstream;
using std::ifstream;
using std::string;

class WordTrans
{
public:
    void read(const string &filename);
    void trans(const string &filename);
private: 
    unordered_map<string, string> _um;
};

void WordTrans::read(const string &filename)
{
    ifstream ifs(filename);
    if(!ifs)
    {
        cerr << "open file " << filename << " error" << endl;
        return;
    }


    string key;
    string line;
    while(ifs >> key, getline(ifs, line))
    {
        if(line.size() > 0)
        {
            _um[key] = line.substr(1);//取子串，去除空格
        }
    }

    ifs.close();
}

void WordTrans::trans(const string &filename)
{
    ifstream ifs(filename);
    if(!ifs)
    {
        cerr << "open file " << filename << " error" << endl;
        return;
    }

    string line;
    while(getline(ifs, line))
    {
        istringstream iss(line);
        string word;
        while(iss >> word)
        {
            auto it = _um.find(word);
            if(it != _um.end())
            {
                cout << it->second;
            }
            else
            {
                cout << word;
            }
            cout << " ";
        }
        cout << endl;
    }

    ifs.close();
}

int main(int argc, char *argv[])
{
    WordTrans wt;
    wt.read("map.txt");
    wt.trans("file.txt");
    return 0;
}

